package activitishizhan.userAndGroup;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class UserAndGroupInUserTask {
    /**
     * 获取流程部署引擎
     *
     */
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
    IdentityService identityService = processEngine.getIdentityService();

    /**
     * 部署流程
     */
    @Test
    public void destory(){
//        Deployment deployment = processEngine.getRepositoryService()
//                .createDeployment()
//                .addClasspathResource("diagrams/userAndGroupInUserTask.bpmn")
//                .name("用户和组在用户任务中的应用")
//                .deploy();
//        System.out.println("部署id" + deployment.getId());
        //启动流程
        ProcessInstance pi = processEngine.getRuntimeService()
                .startProcessInstanceByKey("userAndGroupInUserTask");
        System.out.println("流程实例ID" + pi.getId());
        //根据角色查询任务
        TaskService taskService = processEngine.getTaskService();
        Task task = taskService.createTaskQuery().taskCandidateUser("张三").singleResult();
        taskService.claim(task.getId(),"张三");
        taskService.complete(task.getId());
    }


    @Before
    public void setUp(){
        //创建组
        Group deptLeader = identityService.newGroup("deptLeader");
        deptLeader.setName("部门领导");
        deptLeader.setType("assignment");
        identityService.saveGroup(deptLeader);
        //创建用户
        User user = identityService.newUser("张三");
        user.setFirstName("张");
        user.setLastName("三");
        user.setEmail("1756380197@qq.com");
        identityService.saveUser(user);
        //把用户添加到组
        identityService.createMembership("张三","deptLeader");
    }

    @After
    public void afterInvokeTestMethod(){
        identityService.deleteMembership("张三","deptLeader");
        identityService.deleteGroup("deptLeader");
        identityService.deleteUser("张三");
    }



}
